package com.flower8.controller;

import com.flower8.model.SysAdmin;
import com.flower8.model.SysMenu;
import com.flower8.service.SysAdminService;
import com.flower8.service.SysMenuService;
import com.flower8.utils.PageData;
import com.flower8.utils.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员控制层
 * @author tanglin
 * @Date: 2019/09/03 14:11
 * @Version: 1.0
 */

@RestController
@RequestMapping("/sysadmin")
public class SysAdminController extends BaseController{

	private static final Logger log = LoggerFactory.getLogger("adminLogger");

	@Autowired
	private SysAdminService userService;

	@Autowired
	private SysMenuService sysMenuService;

	/**
	 * 获取当前登录用户
	 * @return
	 */
	@RequestMapping("/current")
	public SysAdmin getCurrentAdmin(){
		logBefore(log, "++++++++++ 查询当前登录账号 ++++++++++");
		SysAdmin sysAdmin =  UserUtil.getCurrentUser();
		logAfter(log);
		return sysAdmin;
	}

	/**
	 * 查询所有管理员
	 * @param request
	 * @return
	 */
	@RequestMapping("/getAdminList")
	public Object getAdminList(HttpServletRequest request){
		logBefore(log, "++++++++++ 查询所有管理员列表 ++++++++++");
		PageData pd = new PageData(request);

		pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
		pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

		Map res = new HashMap();
		try {
			List<SysAdmin> adminList =  userService.getAllAdmin(pd);
			int total = userService.getAllAdminCount(pd);
			res.put("data", adminList);
			res.put("recordsTotal", total);   //dataTable分页需要
			res.put("recordsFiltered", total);//dataTable分页需要
			res.put("draw", pd.get("draw").toString());//dataTable分页需要
			logAfter(log);
		}catch (Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return res;
	}

	/**
	 * 修改账号密码
	 * @param request
	 */
	@RequestMapping("/updatePwd")
	public Object changePassword(HttpServletRequest request) {
		logBefore(log, "++++++++++ 修改管理员密码 ++++++++++");
		PageData pd = new PageData(request);
		Long id = Long.parseLong(pd.getString("id"));
		try{
			String res = userService.changePassword(id, pd.getString("oldPassword"), pd.getString("newPassword"));
			if("".equals(res)){
				logAfter(log);
				return responseResultForSuccess(null, "修改成功");
			}else{
				return responseResultForErrorField("oldPassword", res);
			}
		}catch (Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			return responseResultForError("修改失败");
		}
	}

	/**
	 * 根据账号id查询
	 * @param id 账号id
	 * @return
	 */
	@RequestMapping("/getAdminById")
	public Object getAdminById(@RequestParam Long id){
		logBefore(log, "++++++++++ 查询当前登录账号 ++++++++++");
		SysAdmin sysAdmin =userService.getById(id);
		logAfter(log);
		return sysAdmin;
	}

	/**
	 * 根据账号id查看权限菜单
	 * @param id 账号id
	 * @return
	 */
	@RequestMapping("/getMenuByAdminId")
	public Object getMenuByAdminId(@RequestParam Long id){
		logBefore(log, "++++++++++ 根据账号id查看权限菜单 ++++++++++");
		SysAdmin sysAdmin = userService.getById(id);
		List<SysMenu> sysMenu =sysMenuService.getListByUser(sysAdmin.getPermission());
		logAfter(log);
		return sysMenu;
	}

	/**
	 * 修改账号信息
	 * @param sysAdmin
	 * @return
	 */
	@RequestMapping("/updateAdmin")
	public Object updateAdmin(@RequestBody SysAdmin sysAdmin){
		logBefore(log, "++++++++++ 修改账号信息 ++++++++++");
		try {
			userService.update(sysAdmin);
			logAfter(log);
			return returnSuccessMsg(null,"修改成功");
		}catch (Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			return returnFailureMsg("修改失败");
		}
	}

	/**
	 * 管理员重置密码为：123456
	 * @param id  账号id
	 * @param password  重置密码123456
	 * @return
	 */
	@RequestMapping("/restAdminPwd")
	public Object restAdminPwd(@RequestParam String id,@RequestParam String password ){
		logBefore(log, "++++++++++ 管理员重置密码 ++++++++++");
		SysAdmin sysAdmin = new SysAdmin();
		sysAdmin.setId(Long.parseLong(id));
		sysAdmin.setPassword(password);
		sysAdmin.setStatus(-1);
		try {
			userService.update(sysAdmin);
			logAfter(log);
			return returnSuccessMsg(null,"重置密码成功");
		}catch (Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			return returnFailureMsg("重置密码失败");
		}
	}

	/**
	 * 新增管理员
	 * @param sysAdmin
	 * @return
	 */
	@RequestMapping("/saveAdmin")
	public Object saveAdmin(@RequestBody SysAdmin sysAdmin) {
		logBefore(log, "++++++++++ 修改账号信息 ++++++++++");
		try {
			userService.save(sysAdmin);
			logAfter(log);
			return returnSuccessMsg(null,"新增成功");
		}catch (Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			return returnFailureMsg("新增失败");
		}
	}

	/**
	 * 检查账号是否存在
	 * @param account
	 * @return
	 */
	@RequestMapping("/isExitCheckAdmin")
	public Object isExitCheckAdmin(@RequestParam String account){
		logBefore(log, "++++++++++ 检查账号是否存在 ++++++++++");
		SysAdmin sysAdmin = userService.getUser(account);
		if(sysAdmin != null){
			logAfter(log);
			return returnFailureMsg("账号已存在");
		}else{
			return returnSuccessMsg(null,"账号可用");
		}
	}

	/**
	 * 根据管理员id删除
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteAdminById")
	public Object deleteAdminById(@RequestParam long id) {
		logBefore(log, "++++++++++ 根据管理员id删除，当前删除的id为：" + id + " ++++++++++");
		try {
			userService.delete(id);
			logAfter(log);
			return returnSuccessMsg(null, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return returnFailureMsg("删除失败");
		}
	}

}
